package com.bianmin.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bianmin.Data.Result;
import com.bianmin.Mapper.MessageMapper;
import com.bianmin.Model.Message;
import com.bianmin.coomen.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/message")
public class MessageController {

    @Autowired
    private MessageMapper messageMapper;

    //添加消息
    @PostMapping("add")
    public Result<Boolean> addMessage(@RequestBody Message message) {
        message.setCreateTime(Util.currentTimeString());
        return Result.success(messageMapper.insert(message) != 0);
    }

    //获取消息列表
    @GetMapping("list")
    public Result<List<Message>> messageList(int page, int pageSize) {
        IPage<Message> pageData = new Page<>(page,pageSize);
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        messageMapper.selectPage(pageData,wrapper);
        return Result.success(pageData.getRecords());
    }

    //删除消息
    @PostMapping("delete")
    public Result<Boolean> deleteMessage(@RequestBody Map<String,String> map) {
        String id = map.get("id").toString();
        return Result.success(messageMapper.deleteMessageById(id) == 1);
    }

}
